package com.codeying.servlet.yuyue;

import com.codeying.entity.*;
import com.codeying.servlet.AbsServlet;
import com.codeying.utils.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.*;
import com.codeying.utils.component.*;

/**
 * 查询预约试驾
 */
@WebServlet("/yuyue/list")
public class ListYuyueServlet  extends AbsServlet {

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        LoginUser loginUser = MyUtils.getUser(req);
        req.setCharacterEncoding("utf-8");
        //获取查询条件
        Map<String,String> paramMap = toParamMap(req);
        paramMap.put("pageSize","10");
        if(loginUser.getRole().equals("user")){//查看自己相关的信息
            paramMap.put("userid", String.valueOf(loginUser.getId()));//只能查看和自己相关的内容
        }
        //根据查询条件获取分页的数据
        List<Yuyue> yuyueList = yuyueService.list(paramMap);

        //获取分页
        PagerVO<Yuyue> pagerVO = new PagerVO<>( Integer.parseInt( paramMap.get("pageIndex")),Integer.parseInt( paramMap.get("pageSize")));
        pagerVO.setItemCount(yuyueService.count(paramMap));
        pagerVO.init();
        req.setAttribute("pager",pagerVO);

        List<User> useridFrnList =userService.list(new QueryMap()  );
        req.setAttribute("useridFrnList",useridFrnList );//将外键数据放入request
        List<Car> cidFrnList =carService.list(new QueryMap()  );
        req.setAttribute("cidFrnList",cidFrnList );//将外键数据放入request
        //循环遍历list数据，统计、获取外键数据
        for(Yuyue stAv : yuyueList){
                    //获取外键数据
            stAv .setUseridFrn ( userService.getBy("id", String.valueOf(stAv .getUserid ())) );
                    //获取外键数据
            stAv .setCidFrn ( carService.getBy("id", String.valueOf(stAv .getCid ())) );
                        }
        //如果有信息需要统计，放在这里
        StringBuilder statisticInfo = new StringBuilder();
        req.setAttribute("statisticInfo", statisticInfo.toString());
        req.setAttribute("yuyueList",yuyueList);
        req.getRequestDispatcher("/pages/yuyue-list.jsp").forward(req,resp);
    }

    //获取jsp过来的参数
    public Map<String, String> toParamMap(HttpServletRequest req) {
        //用于存储查询的条件
        Map<String,String> paramMap = new HashMap<>();
        //分页页码
        String pageIndexStr = req.getParameter("pageIndex");
        paramMap.put("pageIndex", pageIndexStr == null ? "1" : pageIndexStr);

            String userid = req.getParameter("userid");
        if(MyUtils.isNotEmpty( userid )){
            paramMap.put("userid",userid);
        }
                String cid = req.getParameter("cid");
        if(MyUtils.isNotEmpty( cid )){
            paramMap.put("cid",cid);
        }
            //获取需要排序的字段
        String[] orderby = req.getParameterValues("orderby");
        if(orderby!=null){
            String orderbyStr = Arrays.toString(orderby);
            paramMap.put("orderby",orderbyStr.substring(1,orderbyStr.length()-1));
        }
        return paramMap;
    }

}

